﻿using UnityEngine;
using System.Collections;

public class LobbyGUIScript : MonoBehaviour {

	NetworkScript network;
	DataScript data;
	//lobbyManager manager;
	
	public LobbyPlaneScript[] planes;
	public Transform selectBox;
	string roomTitle = "";
	string comment = "Lets Play!";
	public HostData selectedHost;
	
	private float timer = 0;
	float refreshFrequency = 8.0f;
	int pageNumber = 1;
	
	// Use this for initialization
	void Start () {
		//lobbyOn = true;
		data = GameObject.FindGameObjectWithTag("DataManager").GetComponent<DataScript>();
		network = GameObject.FindGameObjectWithTag("NetworkManager").GetComponent<NetworkScript>();
		//manager = GameObject.FindGameObjectWithTag("SceneManager").GetComponent<lobbyManager>();
		timer = refreshFrequency;
	}
	
	// Update is called once per frame
	void Update () {
		//planes[0].updatePlane(true);

		if(timer > refreshFrequency)
		{
			//Debug.Log("REFRESH");
			timer = 0;
			network.refreshList();
		}
		else
		{
			timer += Time.deltaTime;
		}
		
	}
	
	
	public void selectHost(HostData _data, Transform location)
	{
		selectedHost = _data;
		selectBox.gameObject.SetActive(true);
		selectBox.parent = null;
		selectBox.transform.position = location.position;
		selectBox.parent = location;
	}
	
	void refreshHost(HostData[] hostData)
	{
        for(int i = 0; i < 4; i++) {
			if(i < hostData.Length)
			{
				planes[i].updatePlane(hostData[i * pageNumber]);
			}
			else
			{
				planes[i].updatePlane(false);
			}
        }
	}
	
	/*void closeLobby()
	{
		lobbyOn = false;
		for(int i = 0; i < 6; i++)
		{
			planes[i].gameObject.SetActive(false);
		}
	}
	
	public void openLobby()
	{
		lobbyOn = true;
		network.refreshList();
		timer = 0;
	}*/
	

	void OnGUI()
	{
		//if (lobbyOn)
		//{
			//IP = GUI.TextField(new Rect(150, 50, 100, 20), IP);
 
		
			GUI.Label(new Rect(10,90,100,20),"name:");
			data.playerName = GUI.TextField(new Rect(90,90,100,20),data.playerName,6);
			
		/*
			GUI.Label(new Rect(10,10,100,20),"Comments:");
			comment = GUI.TextField(new Rect(90,10,100,20),comment,13);
			
			if (GUI.Button(new Rect(10, 40, 120, 30), "Start Server"))
			{
				//closeLobby();
				network.startServer(comment);
			}
			
			if (GUI.Button(new Rect(10, 120, 120, 30), "Join Game"))
			{
				if(selectedHost != null)
				{
					//closeLobby();
					network.joinServer(selectedHost);
				}
			}
			*/
			//else if (GUI.Button(new Rect(10, 50, 120, 30), "Connect"))
			//{
			//	Network.Connect(IP, port);
			//}
			//else if (GUI.Button(new Rect(10, 90, 120, 30), "Request HostList"))
			//{
			//	MasterServer.RequestHostList("CSB_MIRACLE_BETA1012");
			//}
		//}
		//selGridInt = GUI.SelectionGrid(new Rect(125, 25, 100, 30), selGridInt, selStrings, 2);
	}
	
	void OnMasterServerEvent(MasterServerEvent mse)
	{
		if (mse == MasterServerEvent.HostListReceived)
		{
			//Debug.Log("Host List Refreshed");
			//MasterServer.ClearHostList();
			refreshHost(MasterServer.PollHostList());
			MasterServer.ClearHostList();
		}
	}
}
